<?php
namespace app\api\controller;

use app\api\model\AdminUsers;
use app\home\model\Article;
use app\home\model\Demand;
use app\home\model\News;
use app\home\model\Supply;
use think\facade\Cookie;

class Test
{
    public function index()
    {
        $msg = "";
        $user = (Array)json_decode(Cookie::get('user')); 
        if(check_token(Cookie::get('tk'), $user['user_name'])){
            $msg = "Hello ".$user['user_name'].", you have login".microtime(true);
        }else{
            $msg = "Hello custom, you have not login";
        }

        $file = "text.caonima.jpg";
        
        return json(['msg'=> $msg, 'file'=>$file, 'file_name'=>str_replace(strrchr($file, "."),"",$file), 'suffix'=>substr(strrchr($file, '.'), 1)]);
    }

    
    public function checkUserStatus(){
        $token = Cookie::get('tk');
        $user = Cookie::get('user');
        if(isset($token, $user) && check_token($token, $user)){
            return true;
        }
        return false;
    }
    public function delCookie()
    {
        Cookie::delete('tk');
        Cookie::delete('user');
        return json(['status'=>'You were logout']);
    }
    public function getTest(){
        return json(AdminUsers::find(1));
    }

    public function siteMap() {
        $articles = Article::field('hash_str')->where('addtime', '>', '2020-12-09 00:00:00')->select();
        $news = News::field('hash_str')->where('addtime', '>', '2020-12-09 00:00:00')->select();
        $supplies = Supply::field('hash')->where('add_time', '>', '2020-12-09 00:00:00')->select();
        $demands = Demand::field('hash_str')->where('add_time', '>', '2020-12-09 00:00:00')->select();
        
        $all_link = [];
        foreach($articles as $a){
            $hash = $a['hash_str'];
            array_push($all_link, "http://www.quzizhu.com?$hash\n");
        }
        foreach($news as $n){
            $hash = $n['hash_str'];
            array_push($all_link, "http://www.quzizhu.com?$hash\n");
        }
        foreach($supplies as $s){
            $hash = $s['hash'];
            array_push($all_link, "http://www.quzizhu.com?$hash\n");
        }
        foreach($demands as $d){
            $hash = $d['hash_str'];
            array_push($all_link, "http://www.quzizhu.com?$hash\n");
        }

        $file_path = '/wwwroot/';
        $file = fopen($file_path.'1.txt', "w");
        foreach($all_link as $link){
            fwrite($file, $link);
        }
        fclose($file);
        
        
    }



    public function log(){
        $start = microtime(true);
        $str = '**quzizhu** | 172.17.0.1 | [21/Dec/2020:10:23:43 +0800] | "GET /api/test/log HTTP/1.1" | 200 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" | localhost | http';
        
        $baidu = 0;
        $_360 = 0;
        $google = 0;
        $sougou = 0;
        $bing = 0;
        
        for ($i=0; $i < 1000000; $i++) { 
            # code...
            if (!strstr($str, '**quzizhu**'))continue;
            $str_splice = explode(' | ', $str);
            if (!strstr($str, 'spider'))continue;
            $time_str = $str_splice[2];
            $time_str = str_replace('[', '', $time_str);
            $time_str = str_replace(']', '', $time_str);
            // echo date('Y-m-d 00:00:00', strtotime($time_str)).PHP_EOL;
        }
        $end = microtime(true); 
        $cost = $end - $start;
        echo "cost: ". $cost . ' sec';
    }
}
